package com.test.platform.ui.screen

import PublishInfoRepository
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.test.platform.model.PublishInfo
import com.test.platform.model.PublishState

// 添加必要的导入
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.lifecycle.viewmodel.compose.viewModel
import com.test.platform.viewmodel.PublishInfoViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PublishInfoScreen(
    modifier: Modifier = Modifier,
    viewModel: PublishInfoViewModel = viewModel { 
        PublishInfoViewModel(PublishInfoRepository())
    }
) {
    val publishInfos by viewModel.publishInfos.collectAsState()
    val loading by viewModel.loading.collectAsState()
    
    LaunchedEffect(Unit) {
        viewModel.loadPublishInfos()
    }
    
    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = { Text("发布信息") }
            )
        }
    ) { padding ->
        if (loading) {
            CircularProgressIndicator(
                modifier = Modifier
                    .fillMaxSize()
                    .wrapContentSize()
            )
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
            ) {
                items(publishInfos) { info ->
                    PublishInfoCard(info)
                }
            }
        }
    }
}

@Composable
private fun PublishInfoCard(info: PublishInfo) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Column {
                    Text(
                        text = info.name,
                        style = MaterialTheme.typography.titleMedium
                    )
                    Text(
                        text = info.owner,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
                StatusBadge(info.status)
            }
            
//            Divider(modifier = Modifier.padding(vertical = 8.dp))
            
            InfoRow("版本", info.version)
            InfoRow("发布者", info.publisher)
            InfoRow("Commit ID", info.commitId)
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                OutlinedButton(
                    onClick = { /* TODO: 查看详情 */ },
                    modifier = Modifier.padding(end = 8.dp)
                ) {
                    Text("详情")
                }
                Button(onClick = { /* TODO: 发布 */ }) {
                    Text("发布")
                }
            }
        }
    }
}

@Composable
private fun InfoRow(label: String, value: String) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp)
    ) {
        Text(
            text = "$label: ",
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )
        Text(
            text = value,
            style = MaterialTheme.typography.bodyMedium
        )
    }
}

@Composable
private fun StatusBadge(status: String) {
    val publishState = try {
        PublishState.valueOf(status)
    } catch (e: IllegalArgumentException) {
        PublishState.IDLE
    }
    
    val (color, text) = when(publishState) {
        PublishState.IDLE -> Color.Gray to "未发布"
        PublishState.PUBLISHING -> Color.Blue to "发布中"
        PublishState.REVIEWING -> Color.Yellow to "审核中"
        PublishState.FAILED -> Color.Red to "发布失败"
        PublishState.APPROVED -> Color.Green to "已通过"
        PublishState.REJECTED -> Color.Red to "已拒绝"
    }
    
    Surface(
        color = color.copy(alpha = 0.2f),
        shape = MaterialTheme.shapes.small
    ) {
        Text(
            text = text,
            modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
            color = color
        )
    }
}